#Author:Jason Song
#https://github.com/tinymins/MY/wiki/MY-Recount-Data-Structure
from django.shortcuts import render
import pymysql
import json
from django.http import JsonResponse,FileResponse
from django.shortcuts import HttpResponse, render
def word(txt):
    str_1 = list(txt+',')
    li = []
    txt_sta = 0
    txt_fin = 0
    txt_xiaokuohao = 0
    txt_yinhao = 0
    txt_zhongkuohao = 0
    txt_dakuohao = 0
    for i,k in enumerate(str_1):
        # print(i,k)
        if k == ',':
            if txt_xiaokuohao == 0 and txt_yinhao == 0 and txt_zhongkuohao == 0 and txt_dakuohao == 0:
                txt_fin = i
                li.append((txt_sta,txt_fin))
                txt_sta = txt_fin+1
        elif k == '"':
            if txt_yinhao == 0:
                txt_yinhao += 1
            else:
                txt_yinhao -= 1
        elif k == '(':
            txt_xiaokuohao += 1
        elif k == ')':
            txt_xiaokuohao -= 1
        elif k == '[':
            txt_zhongkuohao += 1
        elif k == ']':
            txt_zhongkuohao -= 1
        elif k == '{':
            txt_dakuohao += 1
        elif k == '}':
            txt_dakuohao -= 1
    return li


def word_dic(txt):
    for i,k in enumerate(txt):
        if k == "=":
            return ([txt[1:i-1],txt[i+2:-1]])


def det_dic(txt,k):
    # print(txt)
    ski_content = []
    for i in txt:
        # print(i)
        ski_content_detail = []
        # print(k[i[0]],k[i[1]-1])
        if k[i[0]] == '{' and k[i[1]-1] == '}':
            det_dic_k = 1
        elif k[i[0]] == '[' and k[i[1]-1] == ']':
            det_dic_k = 1
        elif k[i[0]] == '(' and k[i[1]-1] == ')':
            det_dic_k = 1
        elif k[i[0]] == '"' and k[i[1]-1] == '"':
            det_dic_k = 1
        else:
            det_dic_k = 0
        # print(k[i[0] + det_dic_k: i[1] - det_dic_k])
        ski_content_detail_list = word(k[i[0] + det_dic_k:i[1] - det_dic_k])
        for m in ski_content_detail_list:
            ski_content_detail.append(k[i[0] + det_dic_k:i[1] - det_dic_k][m[0]:m[1]])
        ski_content.append(ski_content_detail)
    return ski_content

import os
import uuid
def ana(request):
    files = request.FILES.get("file",None) # 接收前端传递过来的文件
    fil_name = str(uuid.uuid1())
    current_path = os.path.dirname(__file__)        # 当前路径
    file_path = os.path.join(current_path,'jcl_data')
    if not os.path.exists(file_path):               # 文件夹不存在则创建
        os.mkdir(file_path)

    with open(os.path.join(file_path,fil_name+'.jx3dat'),'wb') as fp:    # 写文件
        for i in files.chunks():
            fp.write(i)

    file_path = file_path.replace("\\","/")+'/'
    # print(file_path+fil_name+'.jx3dat')
    with open(file_path+fil_name+'.jx3dat', mode='rb') as d:
    # with open(files, mode='rb') as d:
        content = d.read()
        a = content.decode("gb2312")[7:].strip('{').strip('}')
        # print(a)
        cut_list = word(a)
        content_list = []
        fight_log = {
            'qufu' : "",
            'fight_time' : "",
            'focus' : {},
            'cha':{},
        }
        for i in cut_list:
            content_list.append(a[i[0]:i[1]])
        for k in content_list:
            # print(k)
    #目标名称
            if k[0:3] == "[9]":
                foc_li = word(k[5:-1])
                # print(k[5:-1])
                foc_content = []
                for i in foc_li:
                    foc_content.append(k[5:-1][ i[0] : i[1] ] )
                for i in foc_content:
                    foc_id = word_dic(i)[0]
                    foc_name = word_dic(i)[1]
                    # print(word_dic(i)[1])
                    fight_log['focus'][foc_id] = foc_name
    #玩家信息
            if k[0:4] == "[18]":
                cha_content = k[6:-1]
                # print(cha_content)
                cha_sta = 0
                cha_fin = 0
                for i,k in enumerate(list(cha_content)):
                    if k == "[":
                        cha_sta = i
                    elif  k == "]":
                        cha_fin = i
                        break
                fight_log['cha']['name'] = fight_log['focus'][cha_content[cha_sta+1:cha_fin]]



        # print(fight_log)
        # for i in content_list:
        #     print(i)

        # lis = []
        msg = {
                "data": {'name':fight_log['cha']['name'],
                         'uuid':fil_name,
                         },
                "code": 20000,
            }
        return JsonResponse(msg, safe=False)


def analyse_detail(request):
    res = json.loads(request.body)

    fil_name = res['jcl_uuid']
    current_path = os.path.dirname(__file__)        # 当前路径
    file_path = os.path.join(current_path,'jcl_data')

    file_path = file_path.replace("\\","/")+'/'
    # print(file_path+fil_name+'.jx3dat')
    with open(file_path+fil_name+'.jx3dat', mode='rb') as d:
    # with open(files, mode='rb') as d:
        content = d.read()
        a = content.decode("gb2312")[7:].strip('{').strip('}')
        # print(a)
        cut_list = word(a)
        content_list = []
        fight_log = {
            'qufu' : "",
            'fight_time' : "",
            'focus' : {},
            'cha':{},
            'score':0,
        }

        for i in cut_list:
            content_list.append(a[i[0]:i[1]])
        for k in content_list:
            # print(k)
    #区服
            if k[0:4] == "[19]":
                fight_log['qufu'] = k[6:-1]
                # print(qufu)
    #战斗时间
            if k[0:3] == "[7]":
                fight_log['fight_time'] = k[4:]
                # print(fight_log['fight_time'])
    #目标名称
            if k[0:3] == "[9]":
                foc_li = word(k[5:-1])
                # print(k[5:-1])
                foc_content = []
                for i in foc_li:
                    foc_content.append(k[5:-1][ i[0] : i[1] ] )
                for i in foc_content:
                    foc_id = word_dic(i)[0]
                    foc_name = word_dic(i)[1]
                    # print(word_dic(i)[1])
                    fight_log['focus'][foc_id] = foc_name
    #玩家信息
            if k[0:4] == "[18]":
                cha_content = k[6:-1]
                # print(cha_content)
                cha_sta = 0
                cha_fin = 0
                for i,k in enumerate(list(cha_content)):
                    if k == "[":
                        cha_sta = i
                    elif  k == "]":
                        cha_fin = i
                        break
                fight_log['cha']['name'] = fight_log['focus'][cha_content[cha_sta+1:cha_fin]]
                cha_detail = cha_content[cha_fin+3:-1]
                # print(cha_detail)
                cha_detail_content = []
                for i in word(cha_detail):
                    cha_detail_content.append(cha_detail[i[0]:i[1]])
                fight_log['cha']['zhuangfen'] = cha_detail_content[1]
                # fight_log['cha']['zhuangbei'] = cha_detail_content[2]
    #战斗复盘
            if k[0:4] == "[16]":
                jcl_word = k[6:-1]
                jcl_li = word(k[6:-1])
                jcl_content = det_dic(jcl_li, jcl_word)
                # 打印战斗日志
                for i in jcl_content:
                    print(i)
                # print(jcl_content)
    #效果信息缓存
            if k[0:4] == "[11]":
                ski_word = k[6:-1]
                ski_li = word(k[6:-1])
                # print(ski_li)
                ski_content = det_dic(ski_li, ski_word)
                # print(ski_content)
                skill_dic = {}
                for i in ski_content:
                    # print(i[0])
                    skill_list = []
                    skill_dic[i[0][i[0].find('['):i[0].find(']')+1]] = i[0][i[0].find('{'):]
                for i in skill_dic.keys():
                    if i == '["1,24869,1"]':
                        skill_dic[i] = '{"魂阵范围爆炸技能",true,1,24869,1}'
                    elif i == '["1,24868,1"]':
                        skill_dic[i] = '{"魂阵范围爆炸技能",true,1,24868,1}'
                    elif i == '["1,24869,1"]':
                        skill_dic[i] = '{"输出附魔伤害波动",true,1,22169,1}'
                    elif i == '["1,4877,43"]':
                        skill_dic[i] = '{"推测水BUFF刷新",true,1,4877,43}'
                    elif i == '["2,17743,1"]':
                        skill_dic[i] = '{"灯魂1存在判定",true,2,17743,1}'
                    elif i == '["2,17744,1"]':
                        skill_dic[i] = '{"灯魂2存在判定",true,2,17744,1}'
                    elif i == '["2,17745,1"]':
                        skill_dic[i] = '{"灯魂3存在判定",true,2,17745,1}'
                    elif i == '["2,15436,5"]':
                        skill_dic[i] = '{"破防等级提高",true,2,15436,5}'
                    elif i == '["1,25806,1"]':
                        skill_dic[i] = '{"产生连局",true,1,25806,1}'
                    elif i == '["1,24699,1"]':
                        skill_dic[i] = '{"魂灯范围内被击实际效果",true,1,24699,1}'
                    elif i == '["1,24875,1"]':
                        skill_dic[i] = '{"鬼星开穴火离卦象表现",true,1,24875,1}'
                    elif i == '["1,24876,1"]':
                        skill_dic[i] = '{"鬼星开穴水坎卦象表现",true,1,24876,1}'
                    elif i == '["1,24877,1"]':
                        skill_dic[i] = '{"鬼星开穴山艮卦象表现",true,1,24877,1}'
                    elif i == '["1,24831,1"]':
                        skill_dic[i] = '{"起卦_水坎",true,1,24831,1}'
                    elif i == '["1,24832,1"]':
                        skill_dic[i] = '{"起卦_山艮",true,1,24832,1}'
                    elif i == '["1,24833,1"]':
                        skill_dic[i] = '{"起卦_火离",true,1,24833,1}'
                    elif i == '["2,18328,1"]':
                        skill_dic[i] = '{"起卦秘籍附带减伤",true,2,18328,1}'
                    elif i == '["2,17826,1"]':
                        skill_dic[i] = '{"变卦需求",true,2,17826,1}'
                    elif i == '["1,24721,17"]':
                        skill_dic[i] = '{"临水卦象延迟表现",true,"1,24721,17"}'
                    elif i == '["1,24722,17"]':
                        skill_dic[i] = '{"临山卦象延迟表现",true,1,24722,17}'
                    elif i == '["1,24723,17"]':
                        skill_dic[i] = '{"临火卦象延迟表现",true,1,24723,17}'
                    elif i == '["1,25828,2"]':
                        skill_dic[i] = '{"T0套装衍天宗内功伤害招式加AP",true,1,25828,2}'
                    elif i == '["1,25828,2"]':
                        skill_dic[i] = '{"T0套装衍天宗内功伤害招式加AP",true,1,25828,2}'
                    elif i == '["2,15453,1"]':
                        skill_dic[i] = '{"输出额外伤害触发内置CD",true,2,15453,1}'

                    elif i == '["1,24825,1"]':
                        skill_dic[i] = '{"变卦_水坎变山艮",true,1,24825,1}'
                    elif i == '["1,24826,1"]':
                        skill_dic[i] = '{"变卦_水坎变火离",true,1,24826,1}'
                    elif i == '["1,24827,1"]':
                        skill_dic[i] = '{"变卦_山艮变水坎",true,1,24828,1}'
                    elif i == '["1,24828,1"]':
                        skill_dic[i] = '{"变卦_山艮变火离",true,1,24828,1}'
                    elif i == '["1,24829,1"]':
                        skill_dic[i] = '{"变卦_火离变山艮",true,1,24829,1}'
                    elif i == '["1,24830,1"]':
                        skill_dic[i] = '{"变卦_火离变水坎",true,1,24830,1}'
                    elif i == '["1,24878,1"]':
                        skill_dic[i] = '{"魂阵范围爆炸技能_增益",true,1,24878,1}'
                    elif i == '["2,15455,1"]':
                        skill_dic[i] = '{"输出伤害波动",true,2,15455,1}'
                    elif i == '["2,15455,2"]':
                        skill_dic[i] = '{"输出伤害波动",true,2,15455,2}'
                    elif i == '["2,17823,1"]':
                        skill_dic[i] = '{"卦象_山艮解卦需求",true,2,17823,1}'
                    elif i == '["2,17824,1"]':
                        skill_dic[i] = '{"卦象_水坎解卦需求",true,2,17824,1}'
                    elif i == '["2,17825,1"]':
                        skill_dic[i] = '{"卦象_火离解卦需求",true,2,17825,1}'
                    elif i == '["2,15456,1"]':
                        skill_dic[i] = '{"伤害波动内置CD",true,2,15456,1}'
                    elif i == '["1,22166,2"]':
                        skill_dic[i] = '{"输出附魔额外伤害通用母技能",true,1,22166,2}'
                    # print(i,skill_dic[i])

            # if k[0:3] == "[2]":
            #     print(k)
        # print(fight_log)
        # for i in content_list:
        #     print(i)

        # lis = []
    msg = {
                "data": fight_log,
                "code": 20000,
            }
    return JsonResponse(msg, safe=False)

